<?php
  ini_set('display_errors',1); error_reporting(E_ALL);
  
  if(isset($_POST['submit'])){
  	
  	$file = $_FILES['fileImport'];
  	//var_dump($file);exit;
  	if(is_uploaded_file($file['tmp_name'])){
  	
		include 'Excel/reader.php';

		$excel = new Spreadsheet_Excel_Reader();
		
		$excel->read($file['tmp_name']);    
		
		$x=2;
		$data = array();
		while($x<=$excel->sheets[0]['numRows']) {
		  $y=1;
		  $row = array();
		  while($y<=$excel->sheets[0]['numCols']) {
		  	if($y>6) break;
			$row[] = @$excel->sheets[0]['cells'][$x][$y] ;
			$y++;
		  }
		  $data[] = $row;  
		  $x++;
		}
		var_dump($excel->sheets[0]['cells'][20][4]);
		exit;
		mysql_connect('localhost','root','root10');
		mysql_select_db('idms_ais2008');
		echo '<table border=1>
		          <tr>
		              <th>No</th>
		              <th>Debtur Code</th>
		              <th>Debtur Name</th>
		              <th>Balance USD</th>
		              <th>Balance IDR</th>
		              <th>Konversi USD</th>
		              <th>Balance Date</th>
		              <th>Result</th>
		          </tr>';
		$x = 1;
		foreach($data as $row){
		  $code = $row[1];
		  $SQL = "SELECT CUNAME FROM tbldebtur WHERE CUCODE = '$code'";
		  @list($name) = mysql_fetch_row(mysql_query($SQL));
		  echo '<tr><td align=center>'.($x++).'</td><td align=center>'.$code.'</td>';
		  $result = false;
		  if($name){
		  	$usd = (float) $row[2];
		  	$idr = (float) $row[3];
		  	$conv = (float) $row[4];
		  	$Update = "UPDATE tbldebtur SET BALANCE_USD='$usd',BALANCE_IDR='$idr',
		  	           BALANCE_USD_RUPIAH='$conv',BALANCE_DATE='$row[5]' 
		  	           WHERE CUCODE = '$code'";
		  	mysql_query($Update) or die(mysql_error());
		  	$result = (mysql_affected_rows() == 1);
		  	echo '<td>'.$name.'</td>';
		  }else echo '<td>NOT FOUND</td>';
		  echo '<td>'.$row[2].'</td><td>'.$row[3].'</td><td>'.$row[4].'</td><td>'.@date('d/m/Y',@strtotime($row[5])).'</td>';
		  echo $result ? '<th style="color:green;">SUCCESS</th>' : '<th style="color:red;">FAILED</th>';
		  echo '</tr>';
		}		
		echo '</table>';
		echo '<div style="text-align:center; margin-top: 10px;"><a href="index.html"><b>Kembali</b></a></div>';
    	//echo '<pre>'; var_dump($data);
	}
    
  }
?> 
